package com.kgcx.price.controller;

import com.github.pagehelper.PageInfo;
import com.kgcx.common.bean.RequestData;
import com.kgcx.common.bean.ReturnData;
import com.kgcx.common.controller.BaseController;
import com.kgcx.price.domain.TPriceCaculationRule;
import com.kgcx.price.service.TPriceCaculationRuleService;
import com.kgcx.price.utils.MqttClientUtil;
import com.kgcx.price.utils.SystemVariable;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

@Api(description = "报价规则后台维护接口", tags = "报价规则后台维护接口")
@RestController
@RequestMapping("caculationrule")
public class TPriceCaculationRuleController extends BaseController
{
    @Autowired
    private TPriceCaculationRuleService tPriceCaculationRuleService;

    @Autowired
    MqttClientUtil mqttClientUtil;

    @ApiOperation(value = "报价规则后台查询接口", httpMethod = "GET", notes = "这是用来查询报价规则的接口")
    @ApiImplicitParams({@ApiImplicitParam(name = "cityId", value = "城市Id", paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "carType", value = "车辆类型", paramType = "query", dataType = "Integer")})
    @GetMapping("get")
    public ReturnData<TPriceCaculationRule> get(@RequestParam int cityId, @RequestParam int carType)
    {
        if(StringUtils.isEmpty(cityId))
        {
            return new ReturnData<>(SystemVariable.ERROR_RETURN_CODE, "城市为空");
        }

        if(StringUtils.isEmpty(carType))
        {
            return new ReturnData<>(SystemVariable.ERROR_RETURN_CODE, "车辆类型为空");
        }

        TPriceCaculationRule tPriceCaculationRule = new TPriceCaculationRule();

        tPriceCaculationRule.setCityId(cityId);
        tPriceCaculationRule.setCarType(carType);
        return new ReturnData<>(tPriceCaculationRuleService.getByCityIdCarType(tPriceCaculationRule));
    }

    @ApiOperation(value = "后台新增报价规则接口", httpMethod = "POST", notes = "这是用来新增报价规则的接口")
    @PostMapping("addPriceCaculationRule")
    public ReturnData addPriceCaculationRule(@RequestBody TPriceCaculationRule tPriceCaculationRule)
    {
        if(StringUtils.isEmpty(tPriceCaculationRule.getCarType()))
        {
           return new ReturnData(SystemVariable.ERROR_RETURN_CODE, "车辆类型异常");
        }

        if(StringUtils.isEmpty(tPriceCaculationRule.getCityId()))
        {
            return new ReturnData(SystemVariable.ERROR_RETURN_CODE, "城市异常");
        }

        if(StringUtils.isEmpty(tPriceCaculationRule.getType()))
        {
            return new ReturnData(SystemVariable.ERROR_RETURN_CODE, "报价类型异常");
        }

        return tPriceCaculationRuleService.addPriceCaculationRule(tPriceCaculationRule);
    }

    @ApiOperation(value = "报价规则后台修改接口", httpMethod = "POST", notes = "这是用来修改报价规则的接口")
    @PostMapping("updatePriceCaculationRule")
    public ReturnData updatePriceCaculationRule(@RequestBody TPriceCaculationRule tPriceCaculationRule)
    {
        return tPriceCaculationRuleService.updateByPrimaryKeySelective(tPriceCaculationRule);
    }

    @ApiOperation(value = "获取报价规则列表后台接口", httpMethod = "GET", notes = "这是用来获取报价规则列表后台接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", required = false, paramType = "query", dataType = "int", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "pageSize", value = "页容量", required = false, paramType = "query", dataType = "int", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "orderBy", value = "排序", required = false, paramType = "query", dataType = "String", dataTypeClass = String.class)
    })
    @GetMapping("getPriceCaculationRuleList")
    public ReturnData<PageInfo<TPriceCaculationRule>> getPriceCaculationRuleList()
    {
        return tPriceCaculationRuleService.getPriceCaculationRuleList(this.getRequestData());
    }

    @ApiOperation(value = "获取报价规则列表后台接口(带条件)", httpMethod = "GET", notes = "这是用来获取报价规则(带条件)列表后台接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", required = false, paramType = "query", dataType = "int", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "pageSize", value = "页容量", required = false, paramType = "query", dataType = "int", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "orderBy", value = "排序", required = false, paramType = "query", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "type", value = "报价类型(1：常规价 2：套餐价 3：一口价)", required = false, paramType = "query", dataType = "int", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "cityId", value = "城市id", required = false, paramType = "query", dataType = "int", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "carType", value = "车辆类型", required = false, paramType = "query", dataType = "int", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "status", value = "状态", required = false, paramType = "query", dataType = "int", dataTypeClass = Integer.class)

    })
    @GetMapping("getPriceCaculationRuleListByCondition")
    public ReturnData<PageInfo<TPriceCaculationRule>> getPriceCaculationRuleListByCondition()
    {
        return tPriceCaculationRuleService.getPriceCaculationRuleListByCondition(this.getRequestData());
    }

    @GetMapping("test")
    @ApiImplicitParams({@ApiImplicitParam(name = "dirverId", value = "发布消息服务质量", paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "payload", value = "消息内容", paramType = "query", dataType = "String")})
    public void test(String dirverId,String payload)
    {
//        JSONObject jsonObject = new JSONObject();
//        jsonObject.put("dirverId",System.currentTimeMillis());
//        System.out.println(stringRedisTemplate.opsForList().rightPush("mileageRecord_"+ dirverId, jsonObject.toJSONString()));
//        mqttClientUtil.test(dirverId, payload);
    }

    /**
     * 专给监管平台提供查询
     */
    @ApiOperation(value = "监管平台查询报价规则接口", httpMethod = "GET", notes = "这是用来给监管平台查询报价规则接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "kid", value = "主键ID", required = false, paramType = "query", dataType = "String", dataTypeClass = String.class)
    })
    @GetMapping("getPriceCaculationRule")
    public ReturnData<TPriceCaculationRule> getBaseInfo() {
        RequestData requestData = this.getRequestData();
        TPriceCaculationRule priceCaculationRule =tPriceCaculationRuleService.getPriceCaculationRule(requestData.getLong("kid"));
        return new ReturnData<>(priceCaculationRule);
    }
}
